all repos — caroster @ 97838c3bacc3e38cddd50bf5a0fd9b539fa2cb31

[Octree] Group carpool to your event https://caroster.io

frontend/pages/api/nauth/[...nextauth].js (view raw)

 1import NextAuth from 'next-auth';
 2import CredentialsProvider from 'next-auth/providers/credentials';
 3import GoogleProvider from 'next-auth/providers/google';
 4
 5const {STRAPI_URL = 'http://localhost:1337'} = process.env;
 6
 7export default NextAuth({
 8  providers: [
 9    CredentialsProvider({
10      name: 'Strapi',
11      credentials: {
12        email: {label: 'Email', type: 'text'},
13        password: {label: 'Password', type: 'password'},
14      },
15      async authorize(credentials, req) {
16        const response = await fetch(`${STRAPI_URL}/api/auth/local`, {
17          method: 'POST',
18          headers: {'Content-Type': 'application/json'},
19          body: JSON.stringify({
20            identifier: credentials.email,
21            password: credentials.password,
22          }),
23        });
24        const data = await response.json();
25        if (data?.error?.message === 'Your account email is not confirmed')
26          throw new Error('EmailNotConfirmed');
27        else if (!data?.jwt) return null;
28        const {user, jwt} = data;
29        return {...user, jwt};
30      },
31    }),
32    GoogleProvider({
33      clientId: process.env.GOOGLE_CLIENT_ID,
34      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
35    }),
36  ],
37  session: {
38    jwt: true,
39  },
40  callbacks: {
41    jwt: async params => {
42      const {token, user, account} = params;
43
44      // Google Auth
45      if (account?.provider === 'google') {
46        const strapiUrl = process.env.STRAPI_URL || 'http://localhost:1337';
47        const response = await fetch(
48          `${strapiUrl}/api/auth/${account.provider}/callback?access_token=${account?.access_token}`
49        );
50        const data = await response.json();
51        token.id = data.user.id;
52        token.jwt = data.jwt;
53        token.email = data.user.email;
54        token.username = data.user.firstname;
55        token.lang = data.user.lang?.toLowerCase();
56        token.provider = account.provider;
57        token.userCreatedAt = data.user.createdAt;
58      }
59
60      // Strapi Auth
61      else if (user) {
62        token.id = user.id;
63        token.jwt = user.jwt;
64        token.email = user.email;
65        token.username = user.firstname;
66        token.lang = user.lang?.toLowerCase();
67        token.provider = account.provider;
68        token.userCreatedAt = user.createdAt;
69      }
70
71      return token;
72    },
73    session: async params => {
74      const {session, token} = params;
75      if (session) {
76        session.token = token;
77        session.user.name = token.username;
78        session.user.lang = token.lang;
79      }
80      return session;
81    },
82    async redirect({url, baseUrl}) {
83      // Allows relative callback URLs
84      if (url.startsWith('/')) return `${baseUrl}${url}`;
85      // Allows callback URLs on the same host
86      else if (new URL(url).host === new URL(baseUrl).host) return url;
87      return baseUrl;
88    },
89  },
90  pages: {
91    signIn: '/auth/login',
92    error: '/auth/login',
93  },
94});